/*
* jlb
*/
package com.jlb.user.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.user.entity.User;
import com.jlb.user.entity.UserDeposit;
import com.jlb.user.service.UserDepositSV;
import com.jlb.user.service.UserSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 用户的押金 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/userDeposit")
@Slf4j
@Api(value = "用户的押金控制器", description = "用户的押金控制器")
public class UserDepositCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private UserDepositSV userDepositSV;

    @Resource
    private UserSV userSV;

    /**
     * 查询UserDeposit一个详情信息
     *
     * @param id
     * @param userCode 用户编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询UserDeposit一个详情信息", notes = "查询UserDeposit一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String userCode) {
        if (id == null) {
            return null;
        }
        if (userCode == null) {
            return null;
        }
        UserDeposit userDeposit = userDepositSV.load(id, userCode);
        log.info(JSON.toJSONString(userDeposit));
        return BeanRet.create(true, "查询成功", userDeposit);
    }


    /**
     * 根据条件id查询UserDeposit一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询UserDeposit一个详情信息", notes = "查询UserDeposit一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        UserDeposit userDeposit = userDepositSV.loadById(id);
        log.info(JSON.toJSONString(userDeposit));
        return BeanRet.create(true, "查询成功", userDeposit);
    }

    /**
     * 根据条件userCode查询UserDeposit一个详情信息
     *
     * @param userCode 用户编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询UserDeposit一个详情信息", notes = "查询UserDeposit一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "用户编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/userCode/{userCode}")
    @ResponseBody
    public BeanRet loadByUserCode(@PathVariable String userCode) {
        if (userCode == null) {
            return null;
        }
        UserDeposit userDeposit = userDepositSV.loadByUserCode(userCode);
        log.info(JSON.toJSONString(userDeposit));
        return BeanRet.create(true, "查询成功", userDeposit);
    }


    /**
     * 查询UserDeposit信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询UserDeposit信息集合", notes = "查询UserDeposit信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账户", required = true, paramType = "query"),
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "returnTimeBegin", value = "返还时间 一年以后的时间", paramType = "query"),
            @ApiImplicitParam(name = "returnTimeEnd", value = "返还时间 一年以后的时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(String account, @ApiIgnore UserDeposit userDeposit, Integer curPage, Integer pageSize) {
        Page<UserDeposit> page = new Page<>(pageSize, curPage);
        if (StringUtils.isNotBlank(account)) {
            User user = userSV.loadByAccount(account);
            if (user == null) {
                return BeanRet.create(true, "查询成功", page);
            }
            userDeposit.setUserCode(user.getCode());
        }
        List<UserDeposit> userDeposits = userDepositSV.list(userDeposit, page.genRowStart(), page.getPageSize());
        page.setVoList(userDeposits);
        int total = userDepositSV.count(userDeposit);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }


    /**
     * 查询UserDeposit信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询UserDeposit信息集合", notes = "查询UserDeposit信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String userCode, Integer curPage, Integer pageSize) {
        Page<UserDeposit> page = new Page<UserDeposit>(pageSize, curPage);
        List<UserDeposit> userDeposits = userDepositSV.list(id, userCode, page.genRowStart(), page.getPageSize());
        page.setVoList(userDeposits);
        int total = userDepositSV.count(id, userCode);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计UserDeposit信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计UserDeposit信息数量根据主键", notes = " 统计UserDeposit信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String userCode) {
        return userDepositSV.count(id, userCode);
    }

    /**
     * 统计UserDeposit信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计UserDeposit信息数量", notes = "统计UserDeposit信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query"),
            @ApiImplicitParam(name = "deposit", value = "用户押金", paramType = "query"),
            @ApiImplicitParam(name = "starLevel", value = "星级", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：可返还 Returnable，冻结 Frozen，已返还 Returned", paramType = "query"),
            @ApiImplicitParam(name = "returnTime", value = "返还时间 一年以后的时间", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore UserDeposit userDeposit) {
        if (userDeposit == null) {
            return userDepositSV.count(new HashMap());
        } else {
            return userDepositSV.count(userDeposit);
        }
    }


    /**
     * 创建UserDeposit
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建UserDeposit", notes = "创建UserDeposit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deposit", value = "用户押金", paramType = "query"),
            @ApiImplicitParam(name = "starLevel", value = "星级", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：可返还 Returnable，冻结 Frozen，已返还 Returned", paramType = "query"),
            @ApiImplicitParam(name = "returnTime", value = "返还时间 一年以后的时间", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore UserDeposit userDeposit) {
        userDepositSV.save(userDeposit);
        return BeanRet.create(true, "创建成功", userDeposit);
    }


    /**
     * 修改UserDeposit
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改UserDeposit", notes = "修改UserDeposit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query"),
            @ApiImplicitParam(name = "deposit", value = "用户押金", paramType = "query"),
            @ApiImplicitParam(name = "starLevel", value = "星级", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：可返还 Returnable，冻结 Frozen，已返还 Returned", paramType = "query"),
            @ApiImplicitParam(name = "returnTime", value = "返还时间 一年以后的时间", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore UserDeposit userDeposit) {
        userDepositSV.modify(userDeposit);
        return BeanRet.create(true, "修改成功", userDeposit);
    }

    /**
     * 删除UserDeposit
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除UserDeposit", notes = "删除UserDeposit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String userCode) {
        userDepositSV.delete(id, userCode);
        return BeanRet.create(true, "删除UserDeposit成功");
    }

}
